/**
 * Copyright (C) 2011-2012 Luca Li Greci
 *
 * This file is part of pojo-creator
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.pojocreator.provider.number;

import com.pojocreator.provider.DataProvider;

/**
 * This class implements {@link DataProvider} to provide a generic random
 * number, the number should be an instance of a type that extends
 * {@link Number} (e.g. Integer, Long, etc.)
 * 
 * @author Luca Li Greci
 * @param <T> Data type that extends {@link Number}
 */
public class RandomNumber < T extends Number > implements DataProvider < T > {
    /**
     * Create a constant random generator. The RandomNumeric will delegate to the 
     * generator the implementation details.
     */
    private static final RandomNumberGenerator GENERATOR = new RandomNumberGenerator();

    /**
     * Store the data type in use.
     */
    private Class < T > type;

    /**
     * Create an instance of {@link RandomNumber} for a specific data type.
     * 
     * @param type Class type.
     */
    public RandomNumber(Class < T > type) {
        this.type = type;
    }

    /**
     * Generate a random number based on the type
     * 
     * @return a number or null in case the type is not mapped
     */
    @SuppressWarnings("unchecked")
    @Override
    public T data() {
        return (T) GENERATOR.generate(type);
    }

    /**
     * Return the type of element managed.
     * 
     * @return The type of element managed.
     */
    @Override
    public Class < T > type() {
        return type;
    }

    /**
     * The number of elements returned is 1
     * 
     * @return number of elements returned is 1
     */
    @Override
    public int size() {
        return 1;
    }
}
